<%--
Copyright (C) 2008 Jackson Harper (jackson@ximian.com)

This program is free software; you can redistribute it and/or
modify it under the terms of the GNU General Public License
as published by the Free Software Foundation; either version 2
of the License, or (at your option) any later version.

This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
GNU General Public License for more details.

You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
--%>

<%@ Control Language="C#" ClassName="HUDControl" Inherits="Grr.Web.GrurrahBaseControl" %>
<%@ Register Assembly="Gaia.WebWidgets" Namespace="Gaia.WebWidgets" TagPrefix="gaia" %>

<%@ Import Namespace="Grr.Web" %>
<%@ Import Namespace="Grr.Model" %>


<script runat="server">
    
    protected void SignInButton_Click(object sender, EventArgs e)
    {
        login_window.Visible = true;
    }

    protected void RegisterButton_Click(object sender, EventArgs e)
    {
        register_window.Visible = true;
    }

    protected void SignOutButton_Click(object sender, EventArgs e)
    {
        FormsAuthentication.SignOut();

        RaiseBubbleEvent(this, new LoginChangedEventArgs(String.Empty, false));
    }
    
    public override void OnLoginStatusChanged(GrurrahUser user, bool auth)
    {
        if (auth)
        {
            SignInButton.Visible = false;
            RegisterButton.Visible = false;
            Spanner.Visible = false;

            SignOutButton.Visible = true;
        }
        else
        {
            SignOutButton.Visible = false;

            Spanner.Visible = true;
            SignInButton.Visible = true;
            RegisterButton.Visible = true;
        }
    }

    protected void SubmitLoginButton_Click(object sender, EventArgs e)
    {
        LoginErrorLabel.Visible = false;

        string email = EmailTextBox.Text;
        string pass = PasswordTextBox.Text;
        string user = Membership.GetUserNameByEmail(email);
        
        if (Membership.ValidateUser(user, pass))
        {
            FormsAuthentication.SetAuthCookie(user, RememberLoginCheckbox.Checked);
            FormsAuthentication.Authenticate(user, pass);

            RaiseBubbleEvent(this, new LoginChangedEventArgs(user, true));
            login_window.Visible = false;
        }
        else
        {
            if (user == String.Empty)
                LoginErrorLabel.Text = "Email does not exist.";
            else
                LoginErrorLabel.Text = "Password does not match email.";

            LoginErrorLabel.Visible = true;
        }
    }

    protected void SubmitRegisterButton_Click(object sender, EventArgs e)
    {
        string user = Guid.NewGuid().ToString();
        string email = RegisterEmailTextBox.Text;
        string pass = RegisterPasswordTextBox.Text;
        string friendly = RegisterFriendlyNameTextBox.Text;
        string location = RegisterLocationTextBox.Text;
        
        string reason;

        if (!GrurrahUser.IsEmailValid(email, out reason))
        {
            RegisterErrorMessageLabel.Text = String.Concat("Unable to create account: ", reason);
            RegisterErrorMessageLabel.Visible = true;
            return;
        }

        if (!GrurrahUser.IsPasswordValid(pass, out reason))
        {
            RegisterErrorMessageLabel.Text = String.Concat("Unable to create account: ", reason);
            RegisterErrorMessageLabel.Visible = true;
            return;
        }

        if (!GrurrahUser.IsNameValid(friendly, out reason))
        {
            RegisterErrorMessageLabel.Text = String.Concat("Unable to create account: ", reason);
            RegisterErrorMessageLabel.Visible = true;
            return;
        }
        
        try
        {
            Membership.CreateUser(user, pass, email);

            FormsAuthentication.SetAuthCookie(user, false);
            FormsAuthentication.Authenticate(user, pass);

            RaiseBubbleEvent(this, new LoginChangedEventArgs(user, true));

            // Raising the bubble event will set the user in the session
            GrurrahUser u = Session["CurrentUser"] as GrurrahUser;
            if (u != null)
            {
                u.SetExtraPropertiesForName(RegisterFriendlyNameTextBox.Text, RegisterLocationTextBox.Text, Request.UserHostAddress);
            }
            
            register_window.Visible = false;
        }
        catch (MembershipCreateUserException mce)
        {
            RegisterErrorMessageLabel.Text = String.Concat ("Unable to create account: ", mce.Message);
            RegisterErrorMessageLabel.Visible = true;
        }
    }

    protected void CancelRegisterButton_Click(object sender, EventArgs e)
    {
        CleanupRegisterWindow();
        register_window.Visible = false;
    }

    protected void CancelLoginLinkButton_Click(object sender, EventArgs e)
    {
        CleanupLoginWindow();
        login_window.Visible = false;
    }

    protected void register_window_Closing(object sender, WindowClosingEventArgs e)
    {
        CleanupRegisterWindow();
    }

    protected void login_window_Closing(object sender, WindowClosingEventArgs e)
    {
        CleanupLoginWindow();
    }

    private void CleanupLoginWindow()
    {
        EmailTextBox.Text = String.Empty;
        LoginErrorLabel.Visible = false;
    }

    private void CleanupRegisterWindow()
    {
        RegisterFriendlyNameTextBox.Text = String.Empty;
        RegisterEmailTextBox.Text = String.Empty;
        RegisterLocationTextBox.Text = String.Empty;
        RegisterErrorMessageLabel.Visible = false;
    }
</script>


<gaia:LinkButton ID="SignInButton" Visible="false" runat="server" OnClick="SignInButton_Click">Sign In!</gaia:LinkButton>
<gaia:Label ID="Spanner" Text=" | " Visible="false" runat="server" />
<gaia:LinkButton ID="RegisterButton" Visible="false" runat="server" OnClick="RegisterButton_Click">Register</gaia:LinkButton>
<gaia:LinkButton ID="SignOutButton" Visible="false" runat="server" OnClick="SignOutButton_Click">Sign Out</gaia:LinkButton>


<gaia:window id="login_window"
        Width="300px"
        CssClass="light_green" 
        AutoCenter="true" 
        CanClickEscapeToClose="true" 
        Closable="true" 
        Draggable="false" 
        Maximizable="false" 
        Minimizable="false" 
        Modal="true" 
        Resizable="false" 
        ScaleHeightToContent="true"
        Visible="false"
        runat="server" HeightOfBorders="28" WidthOfBorders="12" OnClosing="login_window_Closing">

        <div align="center" style="margin:15,15,15,15;">
            <table>
                <tr>
                    <td colspan="2" align="center" valign="middle">Sign in to Gurrah</td>
                </tr>
                <tr>
                    <td align="center" colspan="2">&nbsp;</td>
                </tr>
                <tr>
                    <td align="right">Email:</td>
                    <td><gaia:TextBox ID="EmailTextBox" runat="server"></gaia:TextBox></td>
                </tr>
                <tr>
                    <td align="right">Password:</td>
                    <td><gaia:TextBox ID="PasswordTextBox" TextMode="Password" runat="server"></gaia:TextBox></td>
                </tr>
                <tr>
                    <td align="right"><gaia:CheckBox ID="RememberLoginCheckbox" runat="server" /></td>
                    <td>Remember me on this computer</td>
                </tr>
                <tr>
                    <td>&nbsp;</td>
                    <td>
                        <gaia:Button ID="SubmitLoginButton" Text="Sign in" runat="server" OnClick="SubmitLoginButton_Click" /></td>
                </tr>
                <tr>
                    <td colspan="2"><center><gaia:Label ID="LoginErrorLabel" Visible="false" runat="server" ForeColor="Red">Invalid Email or Password</gaia:Label></center></td>
                </tr>
                <tr>
                    <td>&nbsp;</td>
                    <td valign="bottom">
                        <gaia:LinkButton ID="CancelLoginLinkButton" runat="server" OnClick="CancelLoginLinkButton_Click">Cancel</gaia:LinkButton></td>
                </tr>
                <tr>
                    <td colspan="2" align="center" valign="bottom">
                        <gaia:LinkButton ID="CanNotAccessAccountButton" runat="server">I can not access my account</gaia:LinkButton></td>
                </tr>
            </table>
        </div>

</gaia:window>


<gaia:window id="register_window"
        Width="350px"
        CssClass="light_green" 
        AutoCenter="true" 
        CanClickEscapeToClose="true" 
        Closable="true" 
        Draggable="false" 
        Maximizable="false" 
        Minimizable="false" 
        Modal="true" 
        Resizable="false" 
        ScaleHeightToContent="true"
        Visible="false"
        runat="server" HeightOfBorders="28" WidthOfBorders="12" OnClosing="register_window_Closing">

        <div align="center" style="margin:15,15,15,15;">
            <table border="0">
                <tr><td></td><td></td></tr>
                <tr>
                    <td><b>Name:</b>
                    </td>
                    <td valign="top">
                        <gaia:TextBox ID="RegisterFriendlyNameTextBox" runat="server"></gaia:TextBox> <gaia:Image ID="InvalidUserNameImage" ImageUrl="~/media/error.png" Visible="false" runat="server" />
                    </td>
                </tr>
                <tr>
                    <td>
                        <strong>Email:</strong>
                    </td>
                    <td>
                        <gaia:TextBox ID="RegisterEmailTextBox" runat="server"></gaia:TextBox> <gaia:Image ID="InvalidEmailImage" ImageUrl="~/media/error.png" Visible="false" runat="server" /></td>
                </tr>
                <tr>
                    <td><b>Password:</b>
                    </td>
                    <td><gaia:TextBox ID="RegisterPasswordTextBox" TextMode="Password" runat="server"></gaia:TextBox> <gaia:Image ID="InvalidPasswordImage" ImageUrl="~/media/error.png" Visible="false" runat="server" />
                    </td>
                </tr>
                <tr>
                    <td>
                        <strong>Re-enter Password:</strong></td>
                    <td>
                        <gaia:TextBox ID="VerifyPasswordTextbox" runat="server" TextMode="Password"></gaia:TextBox> <gaia:Image ID="InvalidVerifyPasswordImage" ImageUrl="~/media/error.png" Visible="false" runat="server" /></td>
                </tr>
                <tr><td></td><td></td></tr>
                <tr>
                    <td>
                        <strong>Optional Information:</strong></td>
                    <td>
                    </td>
                </tr>
                <tr>
                    <td>
                        Location:</td>
                    <td>
                        <gaia:TextBox ID="RegisterLocationTextBox" runat="server"></gaia:TextBox></td>
                </tr>
                <tr><td></td><td></td></tr>
                <tr>
                    <td>
                        <strong>Terms of Service:</strong></td>
                    <td>
                    </td>
                </tr>
                <tr>
                    <td>
                    </td>
                    <td>
                        <textarea id="TextArea1" readonly cols="20" rows="2">I am the license.</textarea></td>
                </tr>
                <tr><td></td><td>
                    <gaia:Label ID="RegisterErrorMessageLabel" runat="server" Text="Error" Visible="false" ForeColor="Red"></gaia:Label></td>
                    </tr>
                <tr>
                    <td>
                    </td>
                    <td>
                        <gaia:Button ID="SubmitRegisterButton" Text="Register" runat="server" OnClick="SubmitRegisterButton_Click" /></td>
                </tr>
                <tr><td></td><td></td></tr>
                <tr>
                    <td colspan="2" align="center">
                        <gaia:LinkButton ID="CancelRegisterButton" runat="server" OnClick="CancelRegisterButton_Click">Cancel</gaia:LinkButton></td>
                </tr>
            </table>
        </div>
</gaia:window>

